Have fun !

Page last updated 1st November 1999

Maths stuff relating to fractals will be added
in future. In the meantime have you ever wondered
how to do 3D texture-mapping ? Or used the standard
texture-mapping equations without knowing how they
were derived ?

Well as a start on my maths page, here is a method of
deriving the 3D texture-mapping equations.
I realise there's probably a more elegant method but
its been a long time since I did 3D co-ordinate geometry
so I'm using the "simplest" method - simultaneous equations.
The equations below assume a 3D co-ordinate system as used
for Direct3D, ie.

"X" for left/right (positive right)
"Y" for up/down (positive up)
"Z" for away/towards (positive away)
Rotations being positive clockwise around an axis pointing
TOWARDS the viewer.

The variables used are as follows:

x1,y1,z1,x2,y2,z2,x3,y3,z3 are the 3D coords of
3 points on the polygon (clockwise 1,2,3 when facing the viewer)
eg.
P2........
|..|......
|....|....
|......|..
P1-------P3

X,Y,Z are the 3D coords of a point on the polygon
x,y are the 2D coords of the same point on the polygon
F is the "focal length" of our "camera" or "eye"
A is "Alpha", B is "Beta"
For texture mapping we require A (alpha) and B (beta)
in terms of x,y,x1,y1,z1,x2,y2,z2,x3,y3,z3 and F in order
to find u and v in the equations:
u = u1 + A(u2-u1) + B(u3-u1)
v = v1 + A(v2-v1) + B(v3-v1)
where u and v are the vertical (u) and horizontal (v)
coordinates on the source texture for the above point and
u1,v1,u2,v2,u3,v3 are the texture coordinates for the above
three points on the polygon.

We will start with 3 equations for any point on a plane:

(1) X = x1 + A(x2-x1) + B(x3-x1)
(2) Y = y1 + A(y2-y1) + B(y3-y1)
(3) Z = z1 + A(z2-z1) + B(z3-z1)

Plus the two equations for conversion to 2D coords:

(4) x = XF / Z
(5) y = YF / Z

From (4) and (5)

(6) X = xZ / F
(7) Y = yZ / F

From (1) and (6):

xZ / F = x1 + A(x2-x1) + B(x3-x1)

xZ = Fx1 + AF(x2-x1) + BF(x3-x1)

Substituting for Z from (3):

xz1 + Ax(z2-z1) + Bx(z3-z1) = Fx1 + AF(x2-x1) + BF(x3-x1)

A[x(z2-z1) - F(x2-x1)] = Fx1 - xz1 + B[F(x3-x1) - x(z3-z1)]

(8) A = {Fx1 - xz1 + B[F(x3-x1) - x(z3-z1)]} / [x(z2-z1) - F(x2-x1)]

Similarly, from (2) and (7):

yZ / F = y1 + A(y2-y1) + B(y3-y1)

yZ = Fy1 + AF(y2-y1) + BF(y3-y1)

Substituting for Z from (3):

yz1 + Ay(z2-z1) + By(z3-z1) = Fy1 + AF(y2-y1) + BF(y3-y1)

A[y(z2-z1) - F(y2-y1)] = Fy1 - yz1 + B[F(y3-y1) - y(z3-z1)]

(9) A = {Fy1 - yz1 + B[F(y3-y1) - y(z3-z1)]} / [y(z2-z1) - F(y2-y1)]

From (8) and (9):

{Fx1 - xz1 + B[F(x3-x1) - x(z3-z1)]} / [x(z2-z1) - F(x2-x1)]
=
{Fy1 - yz1 + B[F(y3-y1) - y(z3-z1)]} / [y(z2-z1) - F(y2-y1)]

Crossmultiplying and expanding some brackets:

Fx1yz2 - Fx1yz1 - FFx1y2 + FFx1y1 - xyz1z2 + xyz1z1 + Fxy2z1 - Fxy1z1
+ B *
[Fy(x3-x1)(z2-z1) - FF(x3-x1)(y2-y1) - xy(z2-z1)(z3-z1) + Fx(y2-y1)(z3-z1)]
=
Fxy1z2 - Fxy1z1 - FFx2y1 + FFx1y1 - xyz1z2 + xyz1z1 + Fx2yz1 - Fx1yz1
+ B *
[Fx(y3-y1)(z2-z1) - FF(x2-x1)(y3-y1) - xy(z2-z1)(z3-z1) + Fy(x2-x1)(z3-z1)]

Cancelling and dividing throughout by F gives:

x1yz2 - Fx1y2 + xy2z1
+ B *
[y(x3-x1)(z2-z1) - F(x3-x1)(y2-y1) + x(y2-y1)(z3-z1)]
=
xy1z2 - Fx2y1 + x2yz1
+ B *
[x(y3-y1)(z2-z1) - F(x2-x1)(y3-y1) + y(x2-x1)(z3-z1)]

Re-arranging:

B{x[(z3-z1)(y2-y1) - (y3-y1)(z2-z1)] + y[(x3-x1)(z2-z1) - (x2-x1)(z3-z1)]
+ F[(x2-x1)(y3-y1) - (x3-x1)(y2-y1)]}
=
x(y1z2 - y2z1) + y(x2z1 - x1z2) + F(x1y2 - x2y1)

At last we have:
B =
[x(y1z2 - y2z1) + y(x2z1 - x1z2) + F(x1y2 - x2y1)]
/ {x[(y2-y1)(z3-z1) - (z2-z1)(y3-y1)]
+ y[(z2-z1)(x3-x1) - (x2-x1)(z3-z1)]
+ F[(x2-x1)(y3-y1) - (y2-y1)(x3-x1)]}

Simplifying:

B = (Rx + Sy + TF) / (Ix + Jy + KF)

Where:
R = y1z2 - y2z1
S = x2z1 - x1z2
T = x1y2 - x2y1
I = (y2-y1)(z3-z1) - (z2-z1)(y3-y1)
J = (z2-z1)(x3-x1) - (x2-x1)(z3-z1)
K = (x2-x1)(y3-y1) - (y2-y1)(x3-x1)

You may notice that I, J and K are recognizable
as the X, Y and Z values of the "normal" to
the plane of the polzgon as calculated using the 3 points.
NB. NOT the unit normal but the original 3 values.

The derivation of A (alpha) may be done in a similar manner.

Equations (8) and (9) can be re-arranged as:

(10) B = {xz1 - Fx1 + A[x(z2-z1) - F(x2-x1)]} / [F(x3-x1) - x(z3-z1)]
(11) B = {yz1 - Fy1 + A[y(z2-z1) - F(y2-y1)]} / [F(y3-y1) - y(z3-z1)]

Combining (10) and (11):

{xz1 - Fx1 + A[x(z2-z1) - F(x2-x1)]} / [F(x3-x1) - x(z3-z1)]
=
{yz1 - Fy1 + A[y(z2-z1) - F(y2-y1)]} / [F(y3-y1) - y(z3-z1)]

Crossmultiplying and expanding some brackets:

Fxy3z1 - Fxy1z1 - xyz1z3 + xyz1z1 - FFx1y3 + FFx1y1 + Fx1yz3 - Fx1yz1
+ A *
[Fx(z2-z1)(y3-y1) - xy(z2-z1)(z3-z1) - FF(x2-x1)(y3-y1) + Fy(x2-x1)(z3-z1)]
=
Fx3yz1 - Fx1yz1 - xyz1z3 + xyz1z1 - FFx3y1 + FFx1y1 + Fxy1z3 - Fxy1z1
+ A *
[Fy(x3-x1)(z2-z1) - xy(z2-z1)(z3-z1) - FF(x3-x1)(y2-y1) + Fx(z3-z1)(y2-y1)]

Cancelling and dividing throughout by F gives:

xy3z1 - Fx1y3 + x1yz3
+ A *
[x(z2-z1)(y3-y1) - F(x2-x1)(y3-y1) + y(x2-x1)(z3-z1)]
=
x3z1y - Fx3y1 + xz3y1
+ A *
[y(x3-x1)(z2-z1) - F(x3-x1)(y2-y1) + x(z3-z1)(y2-y1)]

Re-arranging:

A{x[(z2-z1)(y3-y1) - (z3-z1)(y2-y1)] + y[(x2-x1)(z3-z1) - (x3-x1)(z2-z1)]
+ F[(x3-x1)(y2-y1) - (x2-x1)(y3-y1)]}
=
x(y1z3 - y3z1) + y(x3z1 - x1z3) + F(x1y3 - x3y1)

And so (reversing signs):

A =
[x(y3z1 - y1z3) + y(x1z3 - x3z1) + F(x3y1 - x1y3)]
/ {x[(y2-y1)(z3-z1) - (z2-z1)(y3-y1)]
+ y[(z2-z1)(x3-x1) - (x2-x1)(z3-z1)]
+ F[(x2-x1)(y3-y1) - (y2-y1)(x3-x1)]}

Simplifying:

A = (Cx + Dy + EF) / (Ix + Jy + KF)

Where I, J and K are as for B (beta) and:
C = y3z1 - y1z3
D = x1z3 - x3z1
E = x3y1 - x1y3

That's all the maths for now !

Main page

Downloads

Silly Jokes

Main Gallery

November 1999 Gallery

"New" Gallery

V1.2 Gallery

"Old" Gallery

Browser problems accessing the galleries ?
OK, try the full non-IE5 gallery

MMFrac entire (non-IE5) Gallery

E-Mail me

E-Mail Dave

This page has been visited times since 28th August 1999.